package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.FinanceSrevice;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cargo/finance")
@Slf4j
public class FinanceController extends BaseController {

    @Reference
    private FinanceSrevice financeSrevice;
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        log.info("执行查询角色列表:");
        FinanceExample example = new FinanceExample();
        FinanceExample.Criteria criteria = example.createCriteria();
        // 降序排序
        example.setOrderByClause("create_time desc");
        // 条件1：company_id
        criteria.andCompanyIdEqualTo(getLoginCompanyId());

        // 根据登陆用户的企业id查询
        PageInfo<Finance> pageInfo = financeSrevice.findByPage(example, pageNum, pageSize);
        // 保存
        request.setAttribute("pageInfo", pageInfo);

        return "cargo/finance/finance-list";
    }

    /**
     * 添加
     */
    @Reference
    private InvoiceService invoiceService;
    @Reference
    private ShippingService shippingService;
    @RequestMapping("/add")
    public String add(String id){

        Invoice invoice = invoiceService.findById(id);
        String shippingOrderId = invoice.getShippingOrderId();
        Shipping shipping = shippingService.findById(shippingOrderId);


        Finance finance = new Finance();
        finance.setInvoiceNo(invoice.getInvoiceNo());

        finance.setCreateTime(invoice.getCreateTime());
        finance.setInvoiceMoney(invoice.getInvoiceMoney());
        finance.setExportNos(invoice.getScNo());
        finance.setDestinationPort(shipping.getPortOfDischar());
        finance.setShipmentPort(shipping.getPortOfLoading());
        finance.setConsignee(shipping.getConsignee());
        request.setAttribute("id",id);
        request.setAttribute("finance",finance);
        return "cargo/finance/finance-add";
    }

    @RequestMapping("/edit")
    public String edit(Finance finance) {

        finance.setCreateDept(getLoginUser().getDeptName());
        finance.setCompanyName(getLoginCompanyName());
        finance.setCompanyId(getLoginCompanyId());





        financeSrevice.save(finance);


        return "redirect:/cargo/finance/list";
    }

    @RequestMapping("/toView")
    public String toView(String id) {
        // 根据id查询
        Finance finance = financeSrevice.findById(id);
        request.setAttribute("finance",finance);
        return "cargo/finance/finance-view";
    }

    /**
     * 倒出pdf
     * @throws Exception
     */
    @RequestMapping("/financeExportPdf")
    public void financeExportPdf(String id) throws Exception {
        //1. 获取pdf模板文件流
        InputStream in =
                session.getServletContext()
                        .getResourceAsStream("/jasper/finance02.jasper");
       // 构造数据: map的key要对应模板中的Parameters参数名称
        Finance finance = financeSrevice.findById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("inputDate", finance.getInputDate());
        map.put("invoiceMoney", finance.getInvoiceMoney());
        map.put("exportNo",finance.getExportNos());
        map.put("destinationPort", finance.getDestinationPort());
        map.put("invoiceNo", finance.getInvoiceNo());
        map.put("createTime", finance.getCreateTime());
        map.put("shipmentPort", finance.getShipmentPort());
        map.put("consignee", finance.getConsignee());

        JasperPrint jasperPrint = JasperFillManager.fillReport(in, map, new JREmptyDataSource());
        //5.Pdf导出
        //设置编码
        response.setCharacterEncoding("utf-8");
        //设置下载响应头,固定格式
        response.setHeader("content-disposition", "attachment;fileName=export.pdf");
        ServletOutputStream out = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, out);
        out.close();


    }
}
